<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 后台黑名单模型
 * @package app\admin\model
 */
class Blacklist extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_black_list';
    protected $observerClass = 'app\admin\event\DeleteCache';
    //查询黑名单是否存在
    public function getOne($data)
    {
    	$where = [
    			'cid' => trim($data['cid']),
    			'black_type' => trim($data['black_type']),
                'phone' => $data['phone']
    		];
    	return $this::get($where);
    }

    //更新
    public function upd($data)
    {
        $data['change_time'] = date('Y-m-d H:i:s',time());
    	return $this::isUpdate(true)->allowField(true)->save($data,['id'=>trim($data['black_id'])]);
    }

    public function addOne($data)
    {
        $userInfo = $this->getChannelInfo($data['cid']);
        $data['company_name'] = $userInfo['company_name'];
        $data['change_time'] = date('Y-m-d H:i:s', time());
    	return $this::isUpdate(false)->save($data);
    }

    //黑名单开关
    public function openOrClose($black_id,$status)
    {
        trace(session('username').'把黑名单id为'.$black_id.'的开启状态改为了'.$status,'info');
        return $this::isUpdate(true)->save(['status' => $status],['id'=>$black_id]);
    }

    public function addAll($data,$arr)
    {
        $userInfo = $this->getChannelInfo($data['cid']);
        $companyName = $userInfo['company_name'];
        foreach ($arr as $val) {
            $where = [
                'black_type' => $data['black_type'],
                'cid' => $data['cid'],
                'phone' =>$val
            ];
            $row = [
                'black_type' => $data['black_type'],
                'cid' => $data['cid'],
                'change_time' => date('Y-m-d H:i:s',time()),
                'phone' =>$val,
                'mark' => $data['mark'],
                'company_name' => $companyName
            ];
            if( !$this::where($where)->find() ){
                $this::insert($row);
            }
        }
        return true;
    }

    public function getPhone()
    {
        return $this::field('phone')->group('phone')->select()->toArray();
    }

    public function getUser()
    {
        return $this::field('username')->group('username')->select()->toArray();
    }

    public function getAll()
    {
        $all = $this::paginate(20);
        return $this->changeType($all);
    }

    public function getWhereAll($data)
    {
        if($data['phone']=='' && $data['cid']!=''){
            $where = [
                'cid' => trim($data['cid'])
            ];
        }elseif($data['phone']!='' && $data['cid']==''){
            $where = [
                'phone' => trim($data['phone'])
            ];
        }elseif($data['phone'] !='' && $data['cid'] !=''){
            $where = [
                'phone' => trim($data['phone']),
                'cid' => trim($data['cid'])
            ];
        }
        $all = $this::where($where)->paginate(20);
        return $this->changeType($all);
    }

    /**
     * [getAlls 条件查询全部数据]
     * @author   .guo
     * @DataTime 2019-05-27
     * @param    [type]     $request [条件数组]
     * @return   [type]              [object]
     */
    public function getAlls($request)
    {
        $where = [];

        if (!empty($request['phone'])) {

            $where['phone'] = trim($request['phone']);
        }

        if (!empty($request['cid'])) {

            $where['cid'] = trim($request['cid']);
        }

        return $this::where($where)
            ->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {

                switch ($item->status) {
                    case '1':
                        $item->status_ = '<span class="layui-badge layui-bg-green status-qy">正常</span>';
                        break;

                    default:
                        $item->status_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }

                switch ($item->black_type) {
                    case 'CL':
                        $item->black_type = '客户级';
                        break;
                    case 'MF':
                        $item->black_type = '交友';
                        break;
                    case 'BH':
                        $item->black_type = '营业厅';
                        break;
                    case 'CC':
                        $item->black_type = '信用卡';
                        break;
                    case 'GW':
                        $item->black_type = '网关级';
                        break;
                    default:
                        $item->black_type = '系统级';
                        break;
                }

            });

    }

    public function changeType($list)
    {
        return $list->each(function($item, $key){
              //处理数据关联账户
              switch ($item->black_type) {
                  case 'CL':
                      $item->black_type = '客户级';
                      break;
                  case 'MF':
                      $item->black_type = '交友';
                      break;
                  case 'BH':
                      $item->black_type = '营业厅';
                      break;
                  case 'CC':
                      $item->black_type = '信用卡';
                      break;
                  case 'GW':
                      $item->black_type = '网关级';
                      break;
                  default:
                      $item->black_type = '系统级';
                      break;
                }
            });
    }

    public function getRow($data)
    {
        $list = $this::where(['id'=>$data])->select();
        return $this->changeType($list);
    }

    //返回用户名
    public function getChannelInfo($data)
    {
        return Db::name('bus_channel')->field('company_name')->where(['channel_id'=> trim($data)])->find();
    }

}